package example;
import java.util.Scanner;

class Complex
{
    private int realPart;
    private int imaginaryPart;
    public int getRealPart()
    {
        return realPart;
    }
    public void setRealPart(int realPart)
    {
        this.realPart = realPart;
    }
    public int getImaginaryPart()
    {
        return imaginaryPart;
    }
    public void setImaginaryPart(int imaginaryPart)
    {
        this.imaginaryPart = imaginaryPart;
    }
    public Complex plus(Complex c)
    {
        int real = this.realPart + c.realPart;
        int imaginary = this.imaginaryPart + c.imaginaryPart;
        Complex result = new Complex();
        result.setRealPart(real);
        result.setImaginaryPart(imaginary);
        return result;
    }
    public Complex minus(Complex c)
    {
        int real = this.realPart - c.realPart;
        int imaginary = this.imaginaryPart - c.imaginaryPart;
        Complex result = new Complex();
        result.setRealPart(real);
        result.setImaginaryPart(imaginary);
        return result;
    }
    public String format()
    {
        if(this.imaginaryPart<0)
            return this.realPart + (this.imaginaryPart + "i");
        else
            return this.realPart + "+" + this.imaginaryPart + "i";
    }
}
public class TestComplex {
    public static void main(String[] args)
    {
        System.out.print("请输入第一个复数(格式:a+bi):>");
        Scanner in = new Scanner(System.in);
        String complex1 = in.nextLine();
        System.out.print("请输入第二个复数(格式:a+bi):>");
        String complex2 = in.nextLine();
        Complex c1 = new Complex();
        Complex c2 = new Complex();
        String[] c1Arr = complex1.split("\\+");
        int real1 = Integer.parseInt(c1Arr[0]);
        int imaginary1 = Integer.parseInt(c1Arr[1].substring(0,1));
        c1.setRealPart(real1);
        c1.setImaginaryPart(imaginary1);
        String[] c2Arr = complex2.split("\\+");
        int real2 = Integer.parseInt(c2Arr[0]);
        int imaginary2 = Integer.parseInt(c2Arr[1].substring(0,1));
        c2.setRealPart(real2);
        c2.setImaginaryPart(imaginary2);
        System.out.println("第一个复数+第二个复数:>" + c1.plus(c2).format());
        System.out.println("第一个复数-第二个复数:>" + c1.minus(c2).format());
    }
}
